package beans;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Aspire
 */
public class User_data {

    private String id;
    private String name;
    private String gender;
    private String birthday;
    private String email;
    private String user_likes_ids;
    private int match;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUser_likes_ids() {
        return user_likes_ids;
    }

    public void setUser_likes_ids(String user_likes_ids) {
        this.user_likes_ids = user_likes_ids;
    }

    public int getMatch() {
        return match;
    }

    public void setMatch(int match) {
        this.match = match;
    }
    

    DBMS d = new DBMS();

    public int registerUser() {
        String sql = "insert into user_detail(id,name,gender,birthday,email,user_likes_ids) values('" + this.id + "','" + this.name + "','" + this.gender + "','" + this.birthday + "','" + this.email + "','" + this.user_likes_ids + "')";
        int n = d.guid(sql);
        return n;
    }

    public ArrayList<User_data> matchAction(int l1, int l2, String gender) {
        String sql = "select * from user_detail";
        int age = 0;
        ResultSet rs = d.gSelect(sql);
        ArrayList<User_data> reslt=new ArrayList<User_data>();
        HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
        HashMap<String, String> name = new HashMap<String, String>();
        List<String> user_likes = null;
        List<String> likes = null;
        List<String> temp = null;
        HashMap<String, Integer> cmp = new HashMap<String, Integer>();

        try {
            while (rs.next()) {
                if (this.id.equals(rs.getString("id"))) {
                    user_likes = Arrays.asList(rs.getString("user_likes_ids").split(","));
                } else {
                    Calendar now = Calendar.getInstance();   // Gets the current date and time
                    int year = now.get(Calendar.YEAR);
                    String user_year_x = rs.getString("birthday");
                    int user_year = Integer.parseInt(user_year_x.substring(6));
                    System.out.println("user year : " + (year - user_year));
                    if ((year - user_year) >= l1 && (year - user_year) <= l2 && rs.getString("gender").equals(gender)) {
                        likes = Arrays.asList(rs.getString("user_likes_ids").split(","));
                        hm.put(rs.getString("id"), likes);
                        name.put(rs.getString("id"), rs.getString("name"));
                    }

                }
            }

            for (String key : hm.keySet()) {
                temp = hm.get(key);
                int match = 0;
                for (int counter = 0; counter < user_likes.size(); counter++) {
                    if (temp.contains(user_likes.get(counter))) {
                        match++;
                    }
                }
                cmp.put(key, match);
            }

            cmp = (HashMap<String, Integer>) sortValues(cmp);

            System.out.println("Top 3 Matches: xxxx");  
            int i = 0;
            for (String key : cmp.keySet()) {
                User_data u=new User_data();
                System.out.println(key);   
                u.setId(key);
                System.out.println(name.get(key));
                u.setName(name.get(key));
                System.out.println(cmp.get(key));
                u.setMatch(cmp.get(key));
                reslt.add(u);
                
//                for(String key1:name.keySet()){
//                    if(key==(key1)){
//                        result.put(name.get(key), cmp.va);
//                    }
//                }                
                i++;
                if (i == 3) {
                    break;
                }
            }
//            result = (HashMap<String, Integer>) sortValues(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reslt;

    }

    Map<String, Integer> sortValues(HashMap<String, Integer> cmp) {
        List<Map.Entry<String, Integer>> entries = new LinkedList<Map.Entry<String, Integer>>(cmp.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        Map<String, Integer> sortedLikes = new LinkedHashMap<String, Integer>();
        for (Map.Entry<String, Integer> entry : entries) {
            sortedLikes.put(entry.getKey(), entry.getValue());
        }

        return sortedLikes;
    }

}
